<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Springmvc知识整理一、Springmvc是什么？- 目前实现mvc模式最好的框架 - spring的后续产品 - spring的子模块，不需要整合 二、 Springmvc的核心组件 DisptcherServlet:前置控制器，对其他组件进行控制交互，降低组件间的耦合度  Handler:处理器，完成具体的业务逻辑，也就是我们编写的Controller中的方法，每一个方法是一个Handl">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/Springmvc%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="Springmvc知识整理一、Springmvc是什么？- 目前实现mvc模式最好的框架 - spring的后续产品 - spring的子模块，不需要整合 二、 Springmvc的核心组件 DisptcherServlet:前置控制器，对其他组件进行控制交互，降低组件间的耦合度  Handler:处理器，完成具体的业务逻辑，也就是我们编写的Controller中的方法，每一个方法是一个Handl">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20200119145836552.png">
<meta property="article:published_time" content="2020-08-07T15:08:02.062Z">
<meta property="article:modified_time" content="2020-07-19T10:36:54.070Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20200119145836552.png">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-后端/Springmvc知识整理" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/Springmvc%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" class="article-date">
  <time datetime="2020-08-07T15:08:02.062Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="Springmvc知识整理"><a href="#Springmvc知识整理" class="headerlink" title="Springmvc知识整理"></a>Springmvc知识整理</h2><h3 id="一、Springmvc是什么？"><a href="#一、Springmvc是什么？" class="headerlink" title="一、Springmvc是什么？"></a>一、Springmvc是什么？</h3><pre><code>- 目前实现mvc模式最好的框架
- spring的后续产品
- spring的子模块，不需要整合</code></pre>
<h3 id="二、-Springmvc的核心组件"><a href="#二、-Springmvc的核心组件" class="headerlink" title="二、 Springmvc的核心组件"></a>二、 Springmvc的核心组件</h3><ol>
<li><p>DisptcherServlet:前置控制器，对其他组件进行控制交互，降低组件间的耦合度</p>
</li>
<li><p>Handler:处理器，完成具体的业务逻辑，也就是我们编写的Controller中的方法，每一个方法是一个Handler</p>
</li>
<li><p>HandlerMapping：将请求映射到不同的Handler中交由Handler处理</p>
</li>
<li><p>HandlerInterceptor:处理器拦截器，可以拦截到Handler的请求并做一些处理，如权限验证</p>
</li>
<li><p>HandlerExecutionChain：处理器执行链,执行Handler时将其与拦截器封装成一个可执行的执行链对象进行操作</p>
</li>
<li><p>HandlerAdapter:处理器适配器，Handler中只有我们写的代码，但是Handler需要实现注解的功能，其就是有HandlerAdapter进行适配执行的，执行过程如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">graph TB</span><br><span class="line">查找handler上的表单或参数等注解 --&gt; 根据注解完成操作 --&gt; 执行Handler --&gt; 根据返回结果相关的注解处理结果为视图或json等</span><br></pre></td></tr></table></figure>



</li>
</ol>
<ol start="7">
<li>ModelAndView: 装载模型数据和视图信息</li>
<li>ViewResovler: 将逻辑视图解析为物理视图，如/user/login.jsp会指向相应的login.jsp并返回给前端</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">graph TB</span><br><span class="line">发送请求至DispatchServlet --&gt; DispatcherServlet通过HandlerMapping将请求映射到一个具体的Handler --&gt; 生成Handler和HandlerInterceptor返回为HandlerExecutionChain --&gt; 通过HandlerAdapter执行Handler --&gt; 返回ModelAndView --&gt; 通过ViewResovler进行解析 --&gt; 返回填充了模型数据的view给前端</span><br></pre></td></tr></table></figure>

<p><img src="C:\Users\xxc、\AppData\Roaming\Typora\typora-user-images\image-20200119145836552.png" alt="image-20200119145836552"></p>
<h3 id="三、springmvc的配置-不重要"><a href="#三、springmvc的配置-不重要" class="headerlink" title="三、springmvc的配置(不重要)"></a>三、springmvc的配置(不重要)</h3><ol>
<li><p>创建Maven工程</p>
</li>
<li><p>配置springmvc依赖(可以通过<a target="_blank" rel="noopener" href="https://mvnrepository.com/artifact/org.springframework/spring-webmvc/5.1.5.RELEASE">此网站</a>搜索依赖)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- https:&#x2F;&#x2F;mvnrepository.com&#x2F;artifact&#x2F;org.springframework&#x2F;spring-webmvc --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-webmvc&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;5.1.5.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>web.xml中配置servlet</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>dispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">   // 这里配置springmvc配置文件加载的位置</span><br><span class="line">   <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath:springmvc.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">   </span><br><span class="line"> <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>dispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写自定义的Handler</p>
</li>
<li><p>配置处理器映射器、ViewResovler（在springmvc.xml中）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--配置处理器映射器--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;</span> &gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;mappings&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">props</span>&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">&quot;myController&quot;</span>&gt;</span>myController<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;/<span class="name">props</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--配置处理器--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;com.originit.mvc.controller.MyController&quot;</span> <span class="attr">id</span>=<span class="string">&quot;myController&quot;</span> /&gt;</span></span><br><span class="line">   </span><br><span class="line">  <span class="comment">&lt;!--配置视图处理器--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span> &gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;prefix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suffix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;.jsp&quot;</span> /&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>运行</p>
</li>
</ol>
<p>遇到了问题：</p>
<ul>
<li><p>l表达式不转换。。。</p>
<p>解决办法:jsp的page标签上添加一个属性isIgnoreEL=false,如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page contentType&#x3D;&quot;text&#x2F;html;charset&#x3D;UTF-8&quot; language&#x3D;&quot;java&quot; isELIgnored&#x3D;&quot;false&quot; %&gt;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<p>以上是通过xml配置进行，如果要通过注解配置的话只需要开启包扫描扫描controller(也称为Handler)即可,同时要对Controller添加注解</p>
<p>配置静态资源访问:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;servlet-mapping&gt;</span><br><span class="line">  &lt;servlet-name&gt;default&lt;&#x2F;servlet-name&gt;</span><br><span class="line">  &lt;url-pattern&gt;*.css&lt;&#x2F;url-pattern&gt;</span><br><span class="line">&lt;&#x2F;servlet-mapping&gt;</span><br></pre></td></tr></table></figure>






      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/Springmvc%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" data-id="ckdkdagv0000jvoutgrfvfrlk" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/Spring%E4%BA%8B%E5%8A%A1/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/Shiro%E6%A1%86%E6%9E%B6%E7%9A%84%E5%AD%A6%E4%B9%A0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>